<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-range-picker :value="time" @change="onAllocateTimeChange"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => { this.pagination.current = 1;this.getDataList()}"> 查询</a-button>
            <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 530 }"
      :columns="columns"
      :rowKey="(row, index) => row + index"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small">
      <span slot="dDate" slot-scope="dDate">{{ timeFormat(dDate) }}</span>
      <span slot="state" slot-scope="text, record">
            <template>
              <a-tag v-if="!record.Whethertodestroy" color="orange">
                {{ record.state }}
             </a-tag>
             <a-tag v-else color="red">
                  {{ record.state }}
               </a-tag>
            </template>
        </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record.id)">查看</a>
          <a-divider v-if="hasPerm('DispatchList.Edit') && hasPerm('DispatchList.Delete')" type="vertical"/>
          <a v-if="hasPerm('DispatchList.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import moment from 'moment'
import EditForm from '@/views/OrderManagement/rdrecord32/EditForm'
const columns = [
  { title: '出库单号', dataIndex: 'code' },
  { title: '客户编码', dataIndex: 'cCusCode' },
  { title: '客户名称', dataIndex: 'cCusName', width: '20%' },
  {
    title: '单据日期',
    dataIndex: 'madedate',
    customRender: value => {
      if (value) {
        return value.substring(0, 10)
      }
    }
  },

  { title: '单据类型', dataIndex: 'vouchertypeName' },
  { title: '业务类型', dataIndex: 'BusName' },
  // { title: '零售来源类型', dataIndex: 'cSource' },
  // { title: '对应业务单号', dataIndex: 'cBusCode' },
  // { title: '仓库编码', dataIndex: 'cWhCode' },
  // { title: '单据日期', dataIndex: 'dDate' },
  // { title: '收发单据号', dataIndex: 'cCode' },
  // { title: '收发类别编码', dataIndex: 'cRdCode' },
  { title: '制单人', dataIndex: 'maker' },
  // { title: '采购类型编码', dataIndex: 'cPTCode' },
  // { title: '销售类型编码', dataIndex: 'cSTCode' },
  { title: '销售类型', dataIndex: 'StyleNmame' },
  { title: '状态', dataIndex: 'state', scopedSlots: { customRender: 'state' }, },
  // { title: '收货地址', dataIndex: 'cShipAddress', width: '20%', ellipsis: true },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '100px', fixed: 'right' }
]

export default {
  components: {
    EditForm
  },
  props: {
    Cid: { require: true, type: String }
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      time: null,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: { condition: 'cCusName' },
      selectedRowKeys: []
    }
  },
  methods: {
    onAllocateTimeChange (dates, dateStrings) {
      this.queryParam.sDate = dateStrings[0]
      this.queryParam.dDate = dateStrings[1]

      this.time = dates
    },
    repossess () {
      // this.queryParam = { condition: 'cCusName', keyword: this.cName }
      this.queryParam.sDate = null
      this.queryParam.dDate = null
      this.time = null
      this.getDataList()
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/InvoiceManagement/ST_RDRecord_b/GetDataListByCustAndDate', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'DLID',
          SortType: this.sorter.order, DeptId: this.DeptId,
          Search: {
            CustomerId: this.Cid,
            StartTime: this.queryParam.sDate,
            EndTime: this.queryParam.dDate
          },
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          this.data.forEach(item => {
            item.state = item.Whethertodestroy ? '作废' : '正常'
          })
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建发货退货单主表信息')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '详情')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          thisObj.$http.post('/OrderManagement/DispatchList/DeleteData', ids).then(resJson => {
            resolve()

            if (resJson.Success) {
              thisObj.$message.success('操作成功!')

              thisObj.getDataList()
            } else {
              thisObj.$message.error(resJson.Msg)
            }
          })
        })
      })
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
